package com.open.shitou.presenter;

import android.content.Context;

import com.open.shitou.bean.Goods;
import com.open.shitou.bean.MyUser;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

import static cn.bmob.v3.Bmob.getApplicationContext;

/**
 * 用户 P层，包含 手机验证码，注册，登录
 */

public class UserPresenter {

    public static final String PHONE_CODE_TEMPLATE = "米涯手机验证";

    /**
     * 手机验证码获取
     *
     * @param number 手机号码 13281002538
     */
    public void requestSMSCode(final String number) {
        BmobSMS.requestSMSCode(number, PHONE_CODE_TEMPLATE, new QueryListener<Integer>() {
            @Override
            public void done(Integer smsId, BmobException ex) {
                if (ex == null) { // 验证码发送成功
                    Logger.d(smsId);
                } else { // 验证码发送失败
                    Logger.d(ex);
                }
            }
        });
    }

    /**
     * 手机验证码验证
     *
     * @param number 手机号码
     * @param code   短信验证码
     */
    public void verifySmsCode(final String number, final String code) {
        BmobSMS.verifySmsCode(number, code, new UpdateListener() {
            @Override
            public void done(BmobException ex) {
                if (ex == null) { // 短信验证码已验证成功
                } else { // 短信验证码已验证失败
                }
            }
        });
    }

    /**
     * 手机根据验证码-登录/注册
     *
     * @param number 手机号码
     * @param code   短信验证码
     */
    public void signOrLogin(final String number, final String code) {
        MyUser user = new MyUser();
        user.setMobilePhoneNumber(number);
        user.signOrLogin(code, new SaveListener<MyUser>() {
            @Override
            public void done(MyUser myUser, BmobException ex) {
                if (ex == null) { // 注册/登录 成功
                    Logger.d(myUser);
                } else { // 失败
                    Logger.d(ex);
                }
            }
        });
    }

    public Context getContext() {
        return getApplicationContext();
    }

}
